<?php

namespace App\Imports;

use App\Models\airline\base\RouteModel;
use App\Models\AirportModel;
use App\Models\CustomerModel;
use App\Models\DictionaryModel;
use DateTime;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use stdClass;

class CusOrderImport implements ToCollection, WithMultipleSheets
{

    public $result;
    public $error;

    public function __construct() {
        $this->result = [];
    }

    public function collection(Collection $rows){
        //大包件数	小包件数	包装	预报出货日期	目的地口岸 货物属性	报关方式	清关派送	预报重量	预报体积	泡重	入仓方式	收件信息	品名
        $error = [];
        foreach($rows as $k => $row) {
            if($k == 0) continue;

            list($big_package, $small_package, $packaging, $forecast_date, $destination, $goods_attr,
                $cust_type, $clearance, $weight, $volume, $weight_bubble, $storage_type,
                $address, $goods_name
            ) = $row;

            if(!in_array(trim($clearance), ['是', '否'])) {
                $error[] = ['row' => $k+1, 'reason' => '清关派送仅支持 是/否'];
            }

            if(!is_integer($big_package) || $big_package < 0) {
                $error[] = ['row' => $k+1, 'reason' => '大包件数必须为整数且不能小于0'];
            }

            if($small_package == '') $small_package = 0;
            if(!is_integer($small_package)) {
                $error[] = ['row' => $k+1, 'reason' => '小包件数必须为整数'];
            }

            $packaging = array_filter(explode(',', $packaging));
            $in_pkgs = [];
            foreach($packaging as $pkg) {
                $e = DictionaryModel::where('type_id', 1)->where('name', $pkg)->first();
                if(!$e) {
                    $error[] = ['row' => $k+1, 'reason' => '包装类型'. $pkg . '不存在'];
                } else {
                    $in_pkgs[] = $e->id;
                }
            }


            if(is_int($forecast_date)){
                $forecast_date = Date::excelToDateTimeObject($forecast_date)->format('Y-m-d');
            }

            if(!DateTime::createFromFormat('Y-m-d', $forecast_date)) {
                $error[] = ['row' => $k+1, 'reason' => '预报出货日期格式错误'];
            }

            $destination_find = RouteModel::where('arrive_port_code', $destination)->first();
            if(!$destination_find) {
                $error[] = ['row' => $k+1, 'reason' => '目的地口岸不存在'];
            }

            //1普货 2特货
            if(!in_array(trim($goods_attr), ['普货', '敏货'])) {
                $error[] = ['row' => $k+1, 'reason' => '货物属性仅支持 普货/敏货'];
            }

            //1非退税 2退税（一般贸易） 3退税（医疗器械）
            if(!in_array($cust_type, ['非退税', '退税(一般贸易)', '退税(医疗器械)'])) {
                $error[] = ['row' => $k+1, 'reason' => '报关方式仅支持 非退税/退税(一般贸易)/退税(医疗器械)'];
            }

            if(!is_numeric($weight) || $weight < 0) {
                $error[] = ['row' => $k+1, 'reason' => '预报重量必须为数字且不能小于0'];
            }

            if($weight_bubble == '') $weight_bubble = 0;

            if(!is_numeric($weight_bubble)) {
                $error[] = ['row' => $k+1, 'reason' => '泡重必须为数字'];
            }

            if(!is_numeric($volume) || $volume < 0) {
                $error[] = ['row' => $k+1, 'reason' => '预报体积必须为数字且不能小于0'];
            }

            $entry_mode = DictionaryModel::where('type_id', 2)->where('name', $storage_type)->first();
            if(!$entry_mode) {
                $error[] = ['row' => $k+1, 'reason' => '入仓方式'. $storage_type . '不存在'];
            }

            if(empty($error)) {
                $cust_types = ['非退税' => 1, '退税(一般贸易)' => 2, '退税(医疗器械)' => 3];
                $this->result[] = [
                    'destination' => $destination,
                    'goods_attr' => trim($goods_attr) == '普货' ? 1 : 2,
                    'clearance_mode' => $cust_types[$cust_type],
                    'bubble_weight' => $weight,
                    'pre_weight' => $weight,
                    'pre_volume' => $volume,
                    'is_clearance' => trim($clearance) == '是' ? 1 : 0,
                    'pre_out_date' => $forecast_date,
                    'big_pkgs' => $big_package,
                    'small_pkgs' => $small_package,
                    'packages' => $in_pkgs,
                    'entry_mode' => $entry_mode->id ?? 0,
                    'address' => $address,
                    'goods_name' => $goods_name,
                ];
            }

        }

        $this->error = $error;
    }

    public function sheets(): array {
        return [ 0 => $this];
    }


}
